package com.wxtf.platform.main.surgery;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.interfaces.CommonsCallProvider;
import com.wxtf.platform.plugins.user.UserBean;
import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.ConstantInfoHelper;
import com.wxtf.platform.tools.AccessDataBaseUtil;
import com.wxtf.platform.tools.FormatUtil;

/**
 * <li>Title: SurSetTimeRole.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.main.surgery</li>
 * <li>Description: 逾期时间规则设置</li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Aug 27, 2012 10:29:02 AM</li>
 * 
 * @author wangzhi
 * @version 1.0.0.0
 * 
 */
public class SurSetTimeRole extends LogicBase implements CommonsCallProvider{
	private static final Log log = LogFactory.getLog(SurSetTimeRole.class);

	public SurSetTimeRole(String jsonArray){

	}
	
	@Override
	public Object run() {
		Object msg = "";
		try {
			if("queryPage".equals(this.getMethodName())) {
				msg = this.select(this.getLogicData());
				return msg;
			} else if("update".equals(this.getMethodName())) {
				msg = this.update(this.getLogicData());
			} else if("set_f".equals(this.getMethodName())) {
				msg = this.set_f();
			} else {
				msg = "\"没有【" + this.getMethodName() + "】该方法\"";
			}

			if(msg == null || "[null]".equals(msg)) {
				msg = "\"\"";
			}
			if(msg.toString().startsWith("{") || msg.toString().startsWith("[")){
				return msg;
			}
			msg = ConstantInfoHelper.generateJSONString(msg.toString());
		} catch(Exception e) {
			msg = ConstantInfoHelper.generateErrorJSONString();
			log.error(e, e);
		} finally {
			this.clear();
		}
		return msg.toString();
	}

	public Object set_f() throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		if("0".equals(user.getUserId())) {
			String curTime = FormatUtil.getCurentDateTime();
			String sql_sel="select count(*) from t_system_config where SC_UID='"+user.getUserId()+"' and SC_KEY ='surTime'";
			if(AccessDataBaseUtil.queryForInt(sql_sel, null) == 0) {	
				String sql_1="insert into t_system_config(SC_UUID,SC_UID,SC_KEY,SC_VALUE,SC_MARKED,SC_WRITE_TIME) values('"+AccessDataBaseUtil.getPKUUID()+"','"+user.getUserId()+"','surTime','01','1','"+curTime+"')";
				String [] sqls={sql_1};
				AccessDataBaseUtil.executeSQLUpdate(sqls);
				return "\"初始化设置成功\"";
			}
			if(AccessDataBaseUtil.queryForInt(sql_sel, null) != 0) {
				String sql_1="update t_system_config set SC_VALUE='01',SC_WRITE_TIME='"+curTime+"' where SC_UID='"+user.getUserId()+"' and SC_KEY='surTime' ";
				String [] sqls={sql_1};
				AccessDataBaseUtil.executeSQLUpdate(sqls);
				return "\"初始化设置成功\"";
			}
			return "{\"result\":false,\"resultmessage\":\"调用失败\",\"resultrecord\":\"初始化设置失败\"}";
		}
		return "{\"result\":false,\"resultmessage\":\"调用失败\",\"resultrecord\":\"只有管理员才拥有此权限\"}";
	}
	
	@Override
	public Object select(Object jsonArray) throws Exception {
		String[] field={"SC_UUID","SC_UID","SC_KEY","SC_VALUE","SC_MARKED","SC_WRITE_TIME"};
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		if("0".equals(user.getUserId())){
			sql = " from  t_system_config  where SC_KEY ='surTime' and  SC_UID in ('0') ";
		} else {
			sql = " from  t_system_config  where SC_KEY ='surTime' and SC_UID in ('0') ";
		}
		String countSql = "select count(*) " + sql+" order by SC_KEY asc";
		String querySql = "select SC_UUID,SC_UID,SC_KEY,SC_VALUE,SC_MARKED,SC_WRITE_TIME " + sql;
		return AccessDataBaseUtil.query4SQLPagging(field, countSql, querySql, this.getPage(), this.getPagesize(), null);
	}
	
	@Override
	public Object insert(Object jsonArray) throws Exception {
		return null;
	}
	
	@Override
	public Object update(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		if("0".equals(user.getUserId())) {
			String curTime = FormatUtil.getCurentDateTime();
			JSONArray array = JSONArray.fromObject(jsonArray);
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			String role_ri  = jsonObject.getString("role_ri");  //获取 日
			String sql_s1 = "";
			sql_s1="update t_system_config set SC_VALUE='"+role_ri+"',SC_WRITE_TIME='"+curTime+"' where SC_UID='"+user.getUserId()+"' and SC_KEY='surTime'";
			String[] sqls = { sql_s1 };
			AccessDataBaseUtil.executeSQLUpdate(sqls);
			return "\"修改逾期规则成功\"";
		}
		return "{\"result\":false,\"resultmessage\":\"调用失败\",\"resultrecord\":\"只有管理员才拥有此权限\"}";
	}

	
	@Override
	public Object delete(Object jsonArray) throws Exception {
		return null;
	}
	
	
	@Override
	public Object recordCount(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object setRequest(HttpServletRequest request) throws Exception {
		this.request = request;
		return request;
	}

	@Override
	public Object updateBatch(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object onCreate(Object jsonArray) {
		return null;
	}

	@Override
	public Object onDestory(Object jsonArray) {
		return null;
	}

	@Override
	public Object onInit(Object jsonArray) {
		return null;
	}
}


